package prastise;

import java.util.LinkedList;
import java.util.Queue;

public class MyStack {

    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    public MyStack() {
        this.queue1 = new LinkedList<>();
        this.queue2 = new LinkedList<>();
    }

    private void addAll(Queue<Integer> dest, Queue<Integer> src, int x) {
        dest.add(x);
        dest.addAll(src);
        src.clear();
    }

    public void push(int x) {
        if(queue1.isEmpty()) {
            addAll(queue1, queue2, x);
        } else {
            addAll(queue2, queue1, x);
        }
    }

    public int pop() {
        if(!queue1.isEmpty()) {
            return queue1.poll();
        } else if(!queue2.isEmpty()){
            return queue2.poll();
        } else {
            return 0;
        }
    }

    public int top() {
        if(!queue1.isEmpty()) {
            return queue1.peek();
        } else if(!queue2.isEmpty()){
            return queue2.peek();
        } else {
            return 0;
        }
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}
